package config

import (
	strings2 "gitee.com/mootok/mootok/src/constant/strings"
	"github.com/caarlos0/env/v6"
	"github.com/joho/godotenv"
	log "github.com/sirupsen/logrus"
	"strings"
)

var EnvConfig envConfig

type envConfig struct {
	Env                        string `env:"ENV"`
	MySqlHost                  string `env:"MYSQL_HOST"`
	MySqlPort                  string `env:"MYSQL_PORT"`
	MySqlUser                  string `env:"MYSQL_USER"`
	MySqlPassword              string `env:"MYSQL_PASSWORD"`
	MySqlDatabase              string `env:"MYSQL_DATABASE"`
	ClickhouseHost             string `env:"CLICKHOUSE_HOST"`
	ClickhousePort             string `env:"CLICKHOUSE_PORT"`
	ClickhouseUser             string `env:"CLICKHOUSE_USER"`
	ClickhousePassword         string `env:"CLICKHOUSE_PASSWORD"`
	ClickhouseDatabase         string `env:"CLICKHOUSE_DB"`
	QiNiuAccessKey             string `env:"QINIU_ACCESSKEY"`
	QiNiuSecretKey             string `env:"QINIU_SECRETKEY"`
	QiNiuKodoBucket            string `env:"QINIU_KODO_BUCKET"`
	QiNiuKodoDomain            string `env:"QINIU_KODO_DOMAIN"`
	StorageProvider            string `env:"STORAGE_PROVIDER" envDefault:"kodo"`
	RedisAddr                  string `env:"REDIS_ADDR"`
	RedisPassword              string `env:"REDIS_PASSWORD"`
	RedisDB                    int    `env:"REDIS_DB"  envDefault:"1"`
	RedisMaster                string `env:"REDIS_MASTER"`
	RedisPrefix                string `env:"REDIS_PREFIX"`
	ApplicationServerPort      int    `env:"APPLICATION_SERVER_PORT" envDefault:"8080"`
	LoggerWithTraceState       string `env:"LOGGER_WITH_TRACE_STATE" envDefault:"enable"`
	ElasticsearchUrl           string `env:"ELASTICSEARCH_URL"`
	SmsServiceProvider         string `env:"SMS_PROVIDER"`
	SmsAccessKey               string `env:"SMS_ACCESSKEY"`
	SmsSecretKey               string `env:"SMS_SECRETKEY"`
	CensorServiceProvider      string `env:"CENSOR_PROVIDER"`
	TranscodingServiceProvider string `env:"TRANSCODING_PROVIDER"`
}

func init() {
	if error := godotenv.Load(".env"); error != nil {
		log.Warnf("Reading env from file failed, attempt loading directly.")
	}

	EnvConfig = envConfig{}

	//format to struct
	if err := env.Parse(&EnvConfig); err != nil {
		panic("Parsing env failed, please check.")
	}
}

func (s *envConfig) IsProduction() bool {
	return strings.ToLower(s.Env) == strings2.EnvProduction
}
